# 定义数据表的序列化器
from django.contrib.auth.models import User
from rest_framework import serializers

from home.models import UserProfile, Category, Question, Answer, VerifyCode, Carousel


# 用户表序列化器
class UserSerializer(serializers.ModelSerializer):
    # 查询用户主表数据的时候不显示
    # 虚拟字段, auth_user数据表中并不存在
    phone=serializers.CharField(write_only=True)
    # 不显示
    password=serializers.CharField(write_only=True)
    class Meta:
        model = User
        # fields中必须再次声明虚拟字段
        fields = '__all__'
        # extra_kwargs = {
        #     # 不显示
        #     'password':{'write_only':True}
        # }

# 用户附加表序列化器
class UserProfileSerializer(serializers.ModelSerializer):
    # 设置父表序列化器
    user = UserSerializer(read_only=True)
    class Meta:
        model = UserProfile
        fields = '__all__'

# 问题分类表序列化器
class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = '__all__'
# 问题表序列化器
class QuestionSerializer(serializers.ModelSerializer):
    # 设置父表关联的序列化器
    cate=CategorySerializer(read_only=True)
    user=UserSerializer(read_only=True)
    # 设置分类cate_id
    cate_id=serializers.IntegerField(write_only=True)
    class Meta:
        model = Question
        fields = '__all__'

# 解答数据表序列化器
class AnswerSerializer(serializers.ModelSerializer):
    # 设置父表序列化器
    que=QuestionSerializer(read_only=True)
    user=UserSerializer(read_only=True)
    # profile = UserProfileSerializer(read_only=True)
    # 设置虚拟字段: 关联的提问id
    que_id=serializers.IntegerField(write_only=True)
    # user_id = serializers.IntegerField(write_only=True,required=False)
    class Meta:
        model=Answer
        fields='__all__'

# 短信验证码数据表序列化器
class VerifyCodeSerializer(serializers.ModelSerializer):
    expires=serializers.DateTimeField(required=False)
    class Meta:
        model = VerifyCode
        fields='__all__'


# 轮播图数据表序列化器
class CarouselSerializer(serializers.ModelSerializer):
    class Meta:
        model = Carousel
        fields='__all__'

